package cn.hua.trip.base.service.impl;

import cn.hua.trip.base.domain.User;
import cn.hua.trip.base.mapper.UserMapper;
import cn.hua.trip.base.query.QueryObject;
import cn.hua.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.queryForLlist(qo);
        return new PageInfo<>(list);
    }

    @Override
    public User get(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(User user) {

        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public User login(String email,String password) {
        User temp = userMapper.selectByEmailAndPassword(email,password);
        if (temp==null) {
            throw new RuntimeException("账号或密码错误");
        }
        return temp;
    }



    @Override
    public void regist(User user) {
        User temp = userMapper.selectByEmailAndPassword(user.getEmail(),null );
        if (temp!=null) {
            throw new RuntimeException("该邮箱已经被注册了");
        }else{
            user.setPassword("111");
            user.setHeadImgUrl("/img/user/head.jpg");
            user.setCoverImgUrl("img/user/bg.jpeg");
            userMapper.insert(user);
        }
    }


}
